let homeData = JSON.parse(
  JSON.stringify(require('../../../public/restfulData/home.json'))
)

//将对象中文键转为英文
function toEnKeys(data, keyMap) {
  return data.map((item) => {
    let obj = {}
    for (let prop in item) {
      if (keyMap.hasOwnProperty(prop)) {
        obj[keyMap[prop]] = item[prop]
      }
    }
    return obj
  })
}

function convertLeftTop(data, target) {
  let keyMap = {
    年份: 'year',
    指数: 'value'
  }
  let chartData = toEnKeys(data, keyMap)
  target.data = chartData
}
function convertLeftCenter(data, target) {
  let keyMap = {
    年份: 'year',
    占比: 'ratio',
    企业户数: 'value'
  }
  let chartData = toEnKeys(data, keyMap)

  let xData = [],
    ratioList = [],
    valueList = []
  chartData.forEach((item) => {
    let { year, ratio, value } = item
    xData.push(year)
    ratioList.push(ratio)
    valueList.push(value)
  })
  target.xData = xData
  target.lineData.data[0].data = ratioList
  target.barData.data[0].data = valueList
}
function convertLeftBottom(data, target) {
  let keyMap = {
    年份: 'year',
    新设占比: 'newRatio',
    关闭占比: 'closeRatio',
    新设企业户数: 'newValue',
    关闭企业户数: 'closeValue'
  }
  let chartData = toEnKeys(data, keyMap)
  let xData = [],
    newRatioList = [],
    closeRatioList = [],
    newValueList = [],
    closeValueList = []
  chartData.forEach((item) => {
    let { year, newRatio, closeRatio, newValue, closeValue } = item
    xData.push(year)
    newRatioList.push(newRatio)
    closeRatioList.push(closeRatio)
    newValueList.push(newValue)
    closeValueList.push(closeValue)
  })

  target.xData = xData
  target.lineData.data[0].data = newRatioList
  target.lineData.data[1].data = closeRatioList
  target.barData.data[0].data = newValueList
  target.barData.data[1].data = closeValueList
}
function convertRightTop(data, target) {
  let keyMap = {
    类型: 'name',
    数量: 'value',
    同比: 'trend'
  }
  let chartData = toEnKeys(data, keyMap)
  target.listData = chartData
}
function convertRightCenter(data, target) {
  let keyMap = {
    年份: 'year',
    占比: 'ratio',
    企业户数: 'value'
  }
  let chartData = toEnKeys(data, keyMap)
  let xData = [],
    ratioList = [],
    valueList = []
  chartData.forEach((item) => {
    let { year, ratio, value } = item
    xData.push(year)
    ratioList.push(ratio)
    valueList.push(value)
  })
  target.xData = xData
  target.lineData[0].data = valueList
  target.lineData[1].data = ratioList
}
function convertCenterBottom(data, target) {
  let keyMap = {
    地区: 'name',
    id: 'id',
    数量: 'value'
  }
  let chartData = toEnKeys(data, keyMap)
  target.centerBottom = chartData
}

function convertHomeJson(data) {
  convertLeftTop(data['产业景气指数'], homeData.leftTop.lineData)
  convertLeftCenter(
    data['存量企业-企业数量'],
    homeData.leftCenter.entNum.seriesData
  )
  convertLeftCenter(
    data['存量企业-注册资本'],
    homeData.leftCenter.registerCapital.seriesData
  )
  convertLeftBottom(
    data['新设关闭-企业数量'],
    homeData.leftBottom.entNum.seriesData
  )
  convertLeftBottom(
    data['新设关闭-注册资本'],
    homeData.leftBottom.registerCapital.seriesData
  )
  convertRightTop(data['企业概览'], homeData.rightTop)
  convertRightCenter(data['上市企业'], homeData.rightCenter.seriesData)
  convertLeftCenter(data['招商引资'], homeData.rightBottom.seriesData)
  convertCenterBottom(data['地图'], homeData)
  return homeData
}

export default convertHomeJson
